package org.edu.jgame.cardgames;

import org.newdawn.slick.*;


public class Card {
	Image[] card = null;
	
	// Position of center card - not corner
	float x = 0.0f;
	float y = 0.0f;	
	
	float scale = 1.0f;
	
	// 2-14: 2-10,J,Q,K,A
	int number = 0;
	// 1-4: Club, Diamond, Spade, Heart
	int type = 0;
	boolean show = false;		// Co hien anh khong
	boolean face = false;		// Mat nao hien len
	
	// Current card
	int idx_card = 0;			// 0-Xap; 1-Ngua	
	
	boolean get_focus = false;	// Bai duoc highlight
	boolean get_chosen = false;	// Bai duoc chon
	boolean enable = true;		// Bai co the tac dong toi duoc khong
	boolean active = true;		// Bai co the dang khong the su dung
	
	static final int FORWARD = 1;
	static final int LEFT = 2;
	static final int RIGHT = 3;
	static final int BACKWARD = 4;
	
	Color pink = null;	
	Color grey = null;
	
	public Card(float x, float y, int number, int type, float rotation, boolean show) throws SlickException {				
		this.x = x;
		this.y = y;
		
		this.number = number;
		this.type = type;
		this.show = show;
		
		card = new Image[2];
		
		card[0] = Player.store.getCard(0).copy();		
		
		card[1] = Player.store.getCard(number*10 + type).copy();
		
		card[0].rotate(rotation);
		card[1].rotate(rotation);
		
		
		idx_card = 0;
		
		
		pink = new Color(255, 182, 193);
		grey = new Color(125, 125, 125);
	}
	
	public void render(GameContainer gameContainer, Graphics graphics) throws SlickException {
		if(show) {			
			
			if(get_focus == true) {
				card[idx_card].draw(x-card[idx_card].getWidth()/2, y-card[idx_card].getHeight()/2, scale, pink);
			}
			else if(active == false && idx_card == 1) {
				card[idx_card].draw(x-card[idx_card].getWidth()/2, y-card[idx_card].getHeight()/2, scale, grey);
			}
			else {
				card[idx_card].draw(x-card[idx_card].getWidth()/2, y-card[idx_card].getHeight()/2, scale);
			}
		}
	}
	
	public void update(GameContainer gameContainer, int delta) throws SlickException {
		
	}
	
	public void getfocus() {
		if(face == true && enable == true && active == true) {
			get_focus = true;
		}
		else {
			get_focus = false;
		}
		
	}
	
	public void losefocus() {
		get_focus = false;
	}
	
	public boolean isfocus() {
		return get_focus;
	}
	
	// Move thay bang ham nay
	public void setPosition(float x, float y) {
		this.x = x;
		this.y = y;
	}
	
	public float getPositionX() {
		return x;
	}
	
	public float getRPositionX() {
		return x - card[idx_card].getWidth()/2;
	}
	
	public float getPositionY() {
		return y;
	}
	
	public float getRPositionY() {
		return y - card[idx_card].getHeight()/2;
	}
	
	// Xoay la bai theo phuong thang 1 goc a
	public void rotate(float degree) {	
		float rotate = card[idx_card].getRotation();
		
		card[0].rotate(360 - rotate + degree);
		card[1].rotate(360 - rotate + degree);		
	
	}
	
	public void addrotate(float degree) {
		card[0].rotate(degree);
		card[1].rotate(degree);
	}
	
	// Khong xai
	public void move(float speed, int direction) {
		switch(direction) {
		case FORWARD:
			y -= speed;
			break;
		case LEFT:
			x -= speed;
			break;
		case RIGHT:
			x += speed;
			break;
		case BACKWARD:
			y += speed;
			break;
		}
	}
	
	
	public void hide(){
		show = false;		
	}
	
	public void show(){
		show = true;		
	}
	
	public void up(){
		face = true;
		idx_card = 1;
	}
	
	public void down(){
		face = false;
		idx_card = 0;
	}	
	
	public boolean isUp() {
		return face;
	}
	
	public boolean isShow() {
		return show;
	}
	
	public Image getImage() {
		return card[idx_card];
	}
	
	public int getType() {
		return type;
	}
	
	public int getValue() {
		return number;
	}
	
	public void setChosen(boolean choice) {
		get_chosen = choice;
	}
	
	public void getChosen() {
		if(face && get_focus) {
			get_chosen = true;
			y -= 20;
		}
	}
	
	public void loseChosen() {
		if(get_chosen) {
			get_chosen = false;
			y += 20;
		}		
	}
	
	public boolean isChosen() {
		return get_chosen;
	}
	
	public void enable() {
		enable = true;
	}
	
	public void disable() {
		enable = false;
		get_chosen = false;
		get_focus = false;
	}
	
	public boolean isEnable() {
		return enable;
	}
	
	public void active() {
		active = true;
	}
	
	public void inactive() {
		active = false;
		//get_chosen = false;
		get_focus = false;
	}
	
	public boolean isActive() {
		return active;
	}
}
